package com.hz.ruoyistorage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hz.rouyigonggong.domain.LgVehicle;
import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.ruoyistorage.domain.LgColdStorage;
import com.hz.ruoyistorage.mapper.LgColdStorageMapper;
import com.hz.ruoyistorage.domain.LgSubCold;
import com.hz.ruoyistorage.service.LgColdStorageService;
import com.hz.ruoyistorage.service.LgSubColdService;
import com.hz.ruoyistorage.service.StorageUserService;
import com.hz.ruoyistorage.utils.ResultJSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @Author gj
 * @Date 2025/3/5 9:40
 * @注释
 */
@RestController
@RequestMapping("/bigCold")
public class LgColdStorageController {
    @Autowired
    private LgColdStorageService lgColdStorageService;
    @Autowired
    private StorageUserService storageUserService;
    @Autowired
    private LgColdStorageMapper lgColdStorageMapper;
    @Autowired
    private LgSubColdService lgSubColdService;

    /**
    * @Description: 卖家app，获取所有大冷库信息
    * @Param: []
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: grx
    * @Date: 17:32 2025/3/12
    */
//    @RequiresPermissions("storage:bigCold:getAll")
    @GetMapping("/getAllBigCold")
    public AjaxResult getAllBigCold(){
        return AjaxResult.success(lgColdStorageService.list(null));
    }
    /**
     * @Description: 卖家app，通过大冷库Id获得对应的小冷库信息
     * @Param: []
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @Author: grx
     * @Date: 17:32 2025/3/12
     */
//    @RequiresPermissions("storage:bigCold:getAll")
    @GetMapping("/getSmallColdById")
    public AjaxResult getSmallColdById(Integer id){
        System.out.println(id+"111111111111111");
        QueryWrapper<LgSubCold> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        return AjaxResult.success(lgSubColdService.list(queryWrapper));
    }

    //获得所有冷库表数据
//    @RequiresPermissions("storage:bigCold:getAll")
    @RequestMapping("/getAll")
    public ResultJSON getAll(String coldName,String coldDescript,String address,
                             String area1,String area2,String totalVolume1,String totalVolume2,
                             Integer status,Integer managerId,Integer pageNum,Integer pageSize){
//        System.out.println(coldName+"---"+coldDescript+"---"+address+"---"+area1+"---"+area2+"-" +
//                "--"+totalVolume1+"---"+totalVolume2+"---"+managerId+"---"+status+"---"+pageNum+"---"+pageSize);
        String createBy = String.valueOf(BaseDomain.getUserId());
        System.out.println("------------------------"+createBy);
        Integer firstNum = (pageNum-1)*pageSize;
        Integer secondNum = pageSize;

//        Map<String, Object> params = new HashMap<>();
//        params.put("coldName", coldName);
//        params.put("coldDescript", coldDescript);
//        params.put("address", address);
//        params.put("area1", area1);
//        params.put("area2", area2);
//        params.put("totalVolume1", totalVolume1);
//        params.put("totalVolume2", totalVolume2);
//        params.put("managerId", managerId);
//        params.put("status", status);
//        params.put("firstNum", firstNum);
//        params.put("secondNum", secondNum);

//        System.out.println(lgColdStorageService.getReallyList(params));
//        return ResultJSON.success(lgColdStorageService.list());
        //完成分页
        return ResultJSON.success(lgColdStorageService.getReallyList(coldName, coldDescript, address, area1,
                area2, totalVolume1, totalVolume2, status,managerId, firstNum, secondNum, createBy));
    }

    //获得单个冷库
//    @RequiresPermissions("storage:bigCold:getAll")
    @GetMapping(value = "/{coldId}")
    public ResultJSON getBigColdById(@PathVariable Integer coldId){
        System.out.println("-----------------------------------"+coldId);
        return ResultJSON.success(lgColdStorageService.getById(coldId));
    }
    //添加冷库

    @PostMapping("/addBigCold")
    public ResultJSON addStorage(@RequestBody LgColdStorage lgColdStorage){
        lgColdStorage.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        lgColdStorage.setRemainingVolume(lgColdStorage.getTotalVolume());
        return ResultJSON.success(lgColdStorageService.save(lgColdStorage));
    }
    //修改冷库

    @PostMapping("/updateBigCold")
    public ResultJSON updateBigCold(@RequestBody LgColdStorage lgColdStorage){
        return ResultJSON.success(lgColdStorageService.updateById(lgColdStorage));
    }
    //获得冷库管理员名单

    @PostMapping("/getStorageUserList")
    public ResultJSON getStorageUserList(){
        return ResultJSON.success(storageUserService.getStorageUserList());
    }

    //获得数据总条数
//    @RequiresPermissions("storage:bigCold:getAll")
    @GetMapping("/getTotal")
    public ResultJSON getTotal(String coldName,String coldDescript,String address,
                               String area1,String area2,String totalVolume1,String totalVolume2,
                               Integer status,Integer managerId){
        String createBy = String.valueOf(BaseDomain.getUserId());
        return ResultJSON.success(lgColdStorageService.getCount(coldName, coldDescript, address, area1,
                area2, totalVolume1, totalVolume2,  status, managerId,createBy));
    }

    //获得父冷库id与名字的对应
    @PostMapping("/getParentCold")
    public ResultJSON getParentCold(){
        String createBy = String.valueOf(BaseDomain.getUserId());
        System.out.println("11111111111111111111111111111111   "+createBy);
        QueryWrapper<LgColdStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("cold_id","cold_name");
        if("1".equals(createBy)||"".equals(createBy) || createBy == null){
            System.out.println("33333333333333333333");
            return ResultJSON.success(lgColdStorageService.list(queryWrapper));
        }else {
            System.out.println("2222222222222222222222222222222222222");
            queryWrapper.eq("manager_id",createBy);
            return ResultJSON.success(lgColdStorageService.list(queryWrapper));
        }

    }
    //获得所有父冷库所有信息，不包含外键

    @PostMapping("/getEveryParent")
    public ResultJSON getEveryParent(){
        return ResultJSON.success(lgColdStorageService.list());
    }
    //获得父冷库信息条数

    @PostMapping("/getParentColdCount")
    public ResultJSON getParentColdCount(){
        return ResultJSON.success(lgColdStorageService.getCount(null,null,null,null,null,null,null,null,null,null));
    }

//    //根据父冷库名字获得负责人信息
//    @PostMapping("/getManagerByColdName")
//    public ResultJSON getManagerByColdName(@RequestBody String coldName){
//        System.out.println(coldName);
//        QueryWrapper<LgColdStorage> queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.select("manager_id").eq("cold_name",coldName);
//        List<LgColdStorage> lgColdStorageList = lgColdStorageService.list(queryWrapper1);
//        LgColdStorage lgColdStorage = lgColdStorageList.get(0);
//        Integer managerId = lgColdStorage.getManagerId();
//        Map<String, Object> params = new HashMap<>();
//        String userName = lgColdStorageService.getUserName(managerId);
//        params.put("userId", managerId);
//        params.put("userName", userName);
//        return ResultJSON.success(params);
//    }
    //根据父冷库id获得负责人信息

    @PostMapping("/getManagerByParentId")
    public ResultJSON getManagerByParentId(@RequestBody Integer parentId){
        QueryWrapper<LgColdStorage> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.select("manager_id").eq("cold_id",parentId);
        List<LgColdStorage> lgColdStorageList = lgColdStorageService.list(queryWrapper1);
        LgColdStorage lgColdStorage = lgColdStorageList.get(0);
        Integer managerId = lgColdStorage.getManagerId();
        Map<String, Object> params = new HashMap<>();
        String userName = lgColdStorageService.getUserName(managerId);
        params.put("userId", managerId);
        params.put("userName", userName);
        return ResultJSON.success(params);
    }

    @GetMapping("/getIdAndName")
    public ResultJSON getIdAndNameList(){
        return ResultJSON.success();
//        return ResultJSON.success(lgColdStorageService.getIdAndName());
    }

//     //逻辑删除
//     @RequiresPermissions("storage:bigCold:delete")
//     @PostMapping(value = "/{coldIds}")
//     public ResultJSON deleteBigCold(@PathVariable Long[] coldIds){
// //        System.out.println("---------------------------------"+coldIds);
//         return ResultJSON.success();
//     }
    //逻辑删除
//@RequiresPermissions("storage:bigCold:delete")
    @PostMapping(value = "/{coldIds}")
    public ResultJSON deleteBigCold(@PathVariable Long[] coldIds){
        for (Long coldId : coldIds){

            lgColdStorageMapper.deleteById(coldId);
        }
        return ResultJSON.success();
    }

    //导出
    @RequiresPermissions("storage:bigCold:export")
    @Log(title = "大冷库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgColdStorage lgColdStorage)
    {

        List<LgColdStorage> list = lgColdStorageService.list(null);
        ExcelUtil<LgColdStorage> util = new ExcelUtil<LgColdStorage>(LgColdStorage.class);
        util.exportExcel(response, list, "冷库数据");
    }



    //根据小冷库id,改变冷库的容积

    @GetMapping("/updateCold")
    public void updateCold(Integer subId,String yunsuan)
    {
        System.out.println("---------------------------------"+subId+"===="+yunsuan);
        //获得小冷库容积
        LgSubCold lgSubCold = lgSubColdService.selectById(subId);
        Double capacity = lgSubCold.getCapacity();
        //获得大冷库容积
        Integer parentId = lgSubColdService.selectById(subId).getParentId();
        LgColdStorage lgColdStorage = lgColdStorageService.getById(parentId);
        Double remainingVolume = lgColdStorage.getRemainingVolume();
        //进行运算
        if(yunsuan.equals("+")){
            lgColdStorage.setRemainingVolume(remainingVolume+capacity);
        }
        if(yunsuan.equals("-")){
            System.out.println(2222222);
            lgColdStorage.setRemainingVolume(remainingVolume-capacity);
            System.out.println("========================"+lgColdStorage.getRemainingVolume());
        }
        System.out.println("+++++++++++++++++++"+lgColdStorage);
        //修改容积
        lgColdStorageMapper.updateCold(lgColdStorage.getRemainingVolume(),lgColdStorage.getColdId());
    }


}
